import {
  RouteLocationNormalizedLoaded as RouteLNL,
  RouteRecordNormalized as RouteRN,
  RouteMeta,
} from "vue-router";

export interface TabPosition {
  left: number;
  top: number;
}

export interface TabRoute {
  name: string;
  path: string;
  scrollPos: TabPosition;
  // type RouteMeta = { title: string; icon: string };
  meta: RouteMeta & AuthRoute.RouteMeta;
}

/** 获取该页签在多页签数据中的索引 */
export const getIndexInTabs = (tabs: Ref<TabRoute[]>, fullPath: string) => {
  return tabs.value.findIndex((tab) => tab.path == fullPath);
};

/** 通过路由名获取该页签在多页签数据中的索引 */
export const getIndexByRouteName = (tabs: TabRoute[], routeName: string) => {
  return tabs.findIndex((tab) => tab.name === routeName);
};

/** route转tab */
export const route2Tab = (
  route: RouteLNL | RouteRN | AuthRoute.Route
): TabRoute => {
  const fullPath = (route as RouteLNL).fullPath || route.path;
  return {
    name: route.name as string,
    path: fullPath,
    meta: route.meta as RouteMeta,
    scrollPos: {
      left: 0,
      top: 0,
    },
  };
};

/** 获取当前路由第一个叶子节点 */
export const getLeafRouter = (routes: AuthRoute.Route[]): AuthRoute.Route => {
  const firstChild = routes?.[0];

  if (firstChild?.children?.length) {
    return getLeafRouter(firstChild.children);
  }
  return firstChild;
};
